package com.itdct.onflow.generator.template.web

import com.itdct.onflow.generator.config.GeneratorConfig
import com.itdct.onflow.generator.model.ClassModel
import com.itdct.onflow.generator.model.CompareResultModel

/**
 * @author Zhouwx
 * @date 2025/8/26 11:47:44
 * @version 1.0
 * @description
 */
class VueEditTemplate(classModel: ClassModel, generatorConfig: GeneratorConfig) : BaseVueTemplate(classModel, generatorConfig) {
    override fun restPath(): String {
        return "${classModel.className}Edit.vue"
    }

    override fun render(): String {
        return """
<script setup lang="tsx">
import Write from './components/${classModel.className}Write.vue'
import { ContentDetailWrap } from '@/components/ContentDetailWrap'
import { ref } from 'vue'
import { useRoute } from 'vue-router'
import { useBaseDetailView } from '@/base/views/BaseDetailView'
import { ${classModel.classNameLowCamel}AddApi, ${classModel.classNameLowCamel}DetailApi, ${classModel.classNameLowCamel}UpdateApi } from '@/api/${getGroupIdPath(classModel.packageName)}${classModel.className}'
import { ${classModel.className}DetailVo, ${classModel.className}EditQo } from '@/api/${getGroupIdPath(classModel.packageName)}${classModel.className}/types'

const writeRef = ref<ComponentRef<typeof Write>>()
const detailView = useBaseDetailView<${classModel.className}DetailVo, ${classModel.className}EditQo>(writeRef, '${classModel.className}')
const { title, loading, baseLoadDetail, baseSave, currentRow, id, back } = detailView

const { query } = useRoute()
const editType = query.type as string

const save = async () => {
  await baseSave(
    async (formData) => {
      await ${classModel.classNameLowCamel}UpdateApi(formData)
    },
    async (formData) => {
      await ${classModel.classNameLowCamel}AddApi(formData)
    }
  )
}

const loadDetail = async () => {
  await baseLoadDetail(${classModel.classNameLowCamel}DetailApi)
}

loadDetail()
</script>

<template>
  <ContentDetailWrap :title="title">
    <Write ref="writeRef" :current-row="currentRow" :edit-type="editType" :edit-id="id" />

    <template #header>
      <BaseButton @click="back">返回</BaseButton>
      <BaseButton type="primary" :loading="loading" @click="save" permission="'save'">
        保存
      </BaseButton>
    </template>
  </ContentDetailWrap>
</template>

        """.trimIndent()
    }

    override fun onModify(compareResultModel: CompareResultModel, content: String): String {
        return content
    }

    override fun subModuleName(): String {
        return "views"
    }
}